package com.nf.health.app.utils;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author jiaxin
 */
public class ViewUtil {
    /**
     * 实例化布局中每个view对象
     *
     * @param viewId
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T getView(Activity act, int viewId) {
        return (T) act.findViewById(viewId);
    }

    /**
     * 实例化布局中每个view对象
     *
     * @param viewId
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T getView(View view, int viewId) {
        return (T) view.findViewById(viewId);
    }


    /**
     * 为TextView设置字符串
     *
     * @param act
     * @param viewId
     * @param resId
     */
    public static void setImageResoure(Activity act, int viewId, int resId) {
        ImageView view = getView(act, viewId);
        view.setImageResource(resId);
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public static void setText(Activity act, int viewId, String text) {
        TextView view = getView(act, viewId);
        view.setText(text);
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public static void setText(View view, int viewId, String text) {
        TextView tv = getView(view, viewId);
        tv.setText(text);
    }

    /**
     * 适配一些图片组件  必须要drawable下的
     *
     * @param resId
     */
    public static void setMyAutoDisplay(Context cnt, Button btn, int resId) {
        Drawable drawable = cnt.getResources().getDrawable(resId);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        btn.setCompoundDrawables(drawable, null, null, null);
    }

    /**
     * 截取所需要字符串的长度
     *
     * @param tv
     * @param content
     * @param maxLine
     * @param act
     * @return
     */
    public static String getSubString(TextView tv, String content, int maxLine, Activity act) {
        float width = tv.getPaint().measureText(content);
        //这里只是为了方便，用屏幕宽度代替了textview控件宽度，如果需要精准控制，可以换成控件宽度
        float tvWidth = act.getWindowManager().getDefaultDisplay().getWidth();
        if (width / tvWidth > (maxLine + 0.3)) {//需要多余0.3行
            double percent = (tvWidth * (maxLine + 0.3)) / width;//要截取总长度的百分比
            return content.substring(0, (int) (content.length() * percent)) + "...";
        }
        return content;
    }

    /**
     * @param textView   textView
     * @param cnt        上下文对象
     * @param textMode   Textmode的左上右下的类型
     * @param backGround ，textView的背景
     * @param poeitionBg textView的旁边的小图片
     * @param padding    旁边小图片距离文字的参数
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setTextImgBackground(TextView textView, Context cnt, TextMode textMode, int backGround, int poeitionBg, int padding) {
        if (textMode != null && poeitionBg != 0) {
            Drawable difPotdrawable = ContextCompat.getDrawable(cnt, poeitionBg);
            difPotdrawable.setBounds(0, 0, 40, 40);
            textView.setCompoundDrawablePadding(padding);
            switch (textMode) {
                case LEFT:
                    textView.setCompoundDrawables(difPotdrawable, null, null, null);
                    break;
                case TOP:
                    textView.setCompoundDrawables(null, difPotdrawable, null, null);
                    break;
                case RIGHT:
                    textView.setCompoundDrawables(null, null, difPotdrawable, null);
                    break;
                case BOTTOM:
                    textView.setCompoundDrawables(null, null, null, difPotdrawable);
                    break;
            }
        }
        if (backGround != 0) {
            Drawable drawableBg = cnt.getResources().getDrawable(backGround);
            textView.setBackground(drawableBg);
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setTextImgBackground3(TextView textView, Context cnt, TextMode textMode, int backGround, int poeitionBg, int padding, int boundSize) {
        if (textMode != null && poeitionBg != 0) {
            Drawable difPotdrawable = ContextCompat.getDrawable(cnt, poeitionBg);
            difPotdrawable.setBounds(0, 0, boundSize, boundSize);
            textView.setCompoundDrawablePadding(padding);
            switch (textMode) {
                case LEFT:
                    textView.setCompoundDrawables(difPotdrawable, null, null, null);
                    break;
                case TOP:
                    textView.setCompoundDrawables(null, difPotdrawable, null, null);
                    break;
                case RIGHT:
                    textView.setCompoundDrawables(null, null, difPotdrawable, null);
                    break;
                case BOTTOM:
                    textView.setCompoundDrawables(null, null, null, difPotdrawable);
                    break;
            }
        }
        if (backGround != 0) {
            Drawable drawableBg = cnt.getResources().getDrawable(backGround);
            textView.setBackground(drawableBg);
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setTextImgBackground2(TextView textView, Context cnt, TextMode textMode, int backGround, int poeitionBg) {
        if (textMode != null && poeitionBg != 0) {
            Drawable difPotdrawable = ContextCompat.getDrawable(cnt, poeitionBg);
            difPotdrawable.setBounds(0, 0, difPotdrawable.getMinimumWidth(), difPotdrawable.getMinimumHeight());
            switch (textMode) {
                case LEFT:
                    textView.setCompoundDrawables(difPotdrawable, null, null, null);
                    break;
                case TOP:
                    textView.setCompoundDrawables(null, difPotdrawable, null, null);
                    break;
                case RIGHT:
                    textView.setCompoundDrawables(null, null, difPotdrawable, null);
                    break;
                case BOTTOM:
                    textView.setCompoundDrawables(null, null, null, difPotdrawable);
                    break;
            }
        }
        if (backGround != 0) {
            Drawable drawableBg = ContextCompat.getDrawable(cnt, backGround);
            textView.setBackground(drawableBg);
        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setTextImgBackground3(TextView textView, Context cnt, int backGround, int leftPosition, int topPosition, int rightPosition, int bottomPosition) {
        Drawable leftPositionNew = null, topPositionNew = null, rightPositionNew = null, bottomPositionNew = null;
        if (leftPosition != 0) {
            leftPositionNew = ContextCompat.getDrawable(cnt, leftPosition);
            leftPositionNew.setBounds(0, 0, leftPositionNew.getMinimumWidth(), leftPositionNew.getMinimumHeight());
        }
        if (topPosition != 0) {
            topPositionNew = ContextCompat.getDrawable(cnt, topPosition);
            topPositionNew.setBounds(0, 0, topPositionNew.getMinimumWidth(), topPositionNew.getMinimumHeight());
        }
        if (rightPosition != 0) {
            rightPositionNew = ContextCompat.getDrawable(cnt, rightPosition);
            rightPositionNew.setBounds(0, 0, rightPositionNew.getMinimumWidth(), rightPositionNew.getMinimumHeight());
        }
        if (bottomPosition != 0) {
            bottomPositionNew = ContextCompat.getDrawable(cnt, bottomPosition);
            bottomPositionNew.setBounds(0, 0, bottomPositionNew.getMinimumWidth(), bottomPositionNew.getMinimumHeight());
        }
        textView.setCompoundDrawables(leftPositionNew, topPositionNew, rightPositionNew, bottomPositionNew);
        if (backGround != 0) {
            Drawable drawableBg = ContextCompat.getDrawable(cnt, backGround);
            textView.setBackground(drawableBg);
        }
    }

    public enum TextMode {
        LEFT(1),
        TOP(2),
        RIGHT(3),
        BOTTOM(4);
        private int color;

        TextMode(int color) {
            this.color = color;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }

        @Override
        public String toString() {
            return super.toString();
        }
    }

    /**
     * 设置TextView的背景颜色
     *
     * @param textView
     * @param bg
     */
    public static void setMyTextViewBgShade(TextView textView, int bg, String content, int colorResId) {
        if (bg != 0) {
            textView.setBackgroundResource(bg);
        }
        textView.setText(content);
        textView.setTextColor(colorResId);
    }

    /**
     * 后台返回的是分要 * 0.01
     *
     * @param textView
     * @param price
     */
    public static void setPriceTextView(TextView textView, String price) {
        Spanned resource = null;
        if (TextUtils.isEmpty(price)) {
            resource = Html.fromHtml("金额： <font color='#FF9F1A'>" + "¥" + "0.00" + "</font>");
        } else {
            float fPrice = Float.parseFloat(price);
            fPrice = (float) (fPrice * 0.01);
            resource = Html.fromHtml("金额： <font color='#FF9F1A'>" + "¥" + Util.saveDecimalTwo(fPrice) + "</font>");
        }
        textView.setText(resource);
    }

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }

    public static void doWaggleAnimation(View view) {
        PropertyValuesHolder rotation = PropertyValuesHolder.ofFloat("Rotation", 0f, -20f, 20f, -20f, 20f, -20f, 20f, -20f, 0f);
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("ScaleX", 0.8f, 0.85f, 0.9f, 1f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("ScaleY", 0.8f, 0.85f, 0.9f, 1f);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, rotation, scaleX, scaleY);
        animator.setDuration(1000);
        animator.setRepeatMode(ValueAnimator.REVERSE);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.start();
    }
}
